﻿namespace OleDbDAL.Processing
{
    using DBUtility;
    using IDal.Processing;
    using Model.Processing;
    using System;
    using System.Data;
    using System.Text;

    public class PackagingFormulaInfoApp : IPackagingFormulaInfo
    {
        private string dbname = "Systems";

        public void Add(PackagingFormulaInfoEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_PackagingFormulaInfo(");
            builder.Append("FormulaId,CommPrice,CommNum,CommId,Operators");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.FormulaId + ",");
            builder.Append("'" + model.CommPrice + "',");
            builder.Append("'" + model.CommNum + "',");
            builder.Append("" + model.CommId + ",");
            builder.Append("'" + model.Operators + "'");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int InfoId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_PackagingFormulaInfo ");
            builder.Append(" where InfoId=" + InfoId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_PackagingFormulaInfo ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by InfoId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "InfoId", "T_PackagingFormulaInfo", swhere);
        }

        public PackagingFormulaInfoEnt GetModel(int InfoId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select InfoId,FormulaId,CommPrice,CommNum,CommId,Operators from T_PackagingFormulaInfo ");
            builder.Append(" where InfoId=" + InfoId);
            PackagingFormulaInfoEnt ent = new PackagingFormulaInfoEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.InfoId = InfoId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["FormulaId"].ToString() != "")
                {
                    ent.FormulaId = int.Parse(table.Rows[0]["FormulaId"].ToString());
                }
                ent.CommPrice = table.Rows[0]["CommPrice"].ToString();
                ent.CommNum = table.Rows[0]["CommNum"].ToString();
                if (table.Rows[0]["CommId"].ToString() != "")
                {
                    ent.CommId = int.Parse(table.Rows[0]["CommId"].ToString());
                }
                ent.Operators = table.Rows[0]["Operators"].ToString();
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_PackagingFormulaInfo ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "InfoId");
        }

        public void Update(PackagingFormulaInfoEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_PackagingFormulaInfo set ");
            builder.Append("FormulaId=" + model.FormulaId + ",");
            builder.Append("CommPrice='" + model.CommPrice + "',");
            builder.Append("CommNum='" + model.CommNum + "',");
            builder.Append("CommId=" + model.CommId + ",");
            builder.Append("Operators='" + model.Operators + "'");
            builder.Append(" where InfoId=" + model.InfoId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

